// JavaScript Document
function sourcePanel(gridId, srcFieldId, addSrcBut, delSrcBut, anchorPercent, inHeight)
{
    // record ================================================
    var grid_row = Ext.data.Record.create(
        [{name: 'src',type: 'string',allowBlank: false}]
    );
	
    // store ======================================================
    var srcStore = new Ext.data.Store
    ({
        autoLoad: false,
        autoDestroy: true,
        reader: new Ext.data.JsonReader({
            id: 'srcId',
            fields: [
                {name: 'srcId', type:'long'},
                {name: 'src', type: 'string'}
            ]
        })
    })
    
    /*var srcStore = new Ext.data.Store
    ({
        data: 
        [["43","http://livelink.exe"],["44","http://yahoo.com"],["45","http://google.com"]],
        reader: new Ext.data.ArrayReader
        ({
            id:'sourceId'
        }, ['sourceId','source'])
    });*/
	
    // field ======================================================
    var inSrc = new Ext.form.TextField
    ({
        id: srcFieldId,
        name: 'Source',
        allowBlank: false
    })
	
    var cm = new Ext.grid.ColumnModel({
        defaults: {
            sortable: true
        },
        columns: [{
            header: 'Source',
            dataIndex: 'src',
            xtype: 'templatecolumn',
            tpl: '<a href="{src}" target="_blank" class="customLink">{src}</a>',
            editor: inSrc
        }]
    });
	
    var sm = new Ext.grid.RowSelectionModel
    ({
        singleSelect:false
    });
	
    var grid = new Ext.grid.EditorGridPanel({
        id: gridId,
        layout:'fit',
        height: inHeight,
        frame: false,
        //enableColumnMove: false, // to prevent invalidating the wrong column
        viewConfig: {
            forceFit: true
        },
        store: srcStore,
        autoExpandColumn : 1,
        cm: cm,
        sm: sm,
        tbar: [{
            id: addSrcBut,
            icon: 'images/add.gif',
            text: 'Add Source',
            handler: function(){
                grid.stopEditing();
                grid.getStore().insert(grid.getStore().getCount(), new grid_row({'src':'http://'}));
                grid.getView().refresh();
                grid.getSelectionModel().selectRow(grid.getStore().getCount()-1);
                grid.startEditing(grid.getStore().getCount()-1,0);
            }
        },'-',{
            id: delSrcBut,
            icon: 'images/delete.gif',
            text: 'Remove Source',
            handler: function(){
                var gridSelectionModel = grid.getSelectionModel();
                var selected = gridSelectionModel.getSelections();
                if(selected.length > 0) 
                {
                    for(var i = 0, r; r = selected[i]; i++) 
                    {
                        srcStore.remove(r);
                    }
                }
            }
        }]
    });
	
    var fieldSet = new Ext.form.FieldSet({
        frame:true,
        anchor: anchorPercent,
        padding: '8 0 0 0',
        defaults: {
            allowBlank: false,
            selectOnFocus: true
            //,msgTarget: 'side'
        },
        items: [grid]
    });

    return fieldSet;
};

function validateSrc(gridId)
{
    var errorArr = [];
    
    var srcGrid = Ext.ComponentMgr.get(gridId);
    var srcGridStore = srcGrid.store;
    var srcErrAdded = false;
    for(i=0; i < srcGridStore.getCount(); i++)
    {
        //var errorCombo = Ext.ComponentMgr.get("srcGrid");
        
        //var srcRow = srcGridStore.data.items[i];
        //var srcCell = srcGridStore.data.items[i].data[0];
        var srcUrl = srcGridStore.data.items[i].data["src"];
		
        if(!Ext.form.VTypes.url(srcUrl))
        {
            var cell = srcGrid.getView().getCell(i,srcGrid.getColumnModel().findColumnIndex('src'));
            if(cell.className.indexOf(' x-form-invalid')==-1)
            {
                cell.className += ' x-form-invalid';
            }
            //cell.classList.add('x-form-invalid'); // doesn't work for IE6
            //cell.innerHTML = 'ext:qtip="Please"; ext:qclass="x-form-invalid-tip"';
            //srcGridStore.data.items[i].error = "Please enter valid Source URL";
            if(!srcErrAdded)
            {
                errorArr.push('- Please enter valid Source URL');
                srcErrAdded = true;
            }
        }
		
        /*if(!validateURL(srcUrl))
        {
                errorArr.push('- Please enter valid Source URL');
                break;
        }*/
    }
    return errorArr;
}